package com.easy.dfc.web;

import com.alibaba.fastjson.JSONObject;
import com.easy.core.web.BaseController;
import com.easy.dfc.Utils.JsonUtil;
import com.easy.dfc.entity.TagGroupSet;
import com.easy.dfc.entity.TagSet;
import com.easy.dfc.service.TagGroupSetService;
import com.easy.dfc.Utils.Error;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping(value = "${apiPath}/tag", method = RequestMethod.POST)
public class TageController extends BaseController {

    @Resource
    private TagGroupSetService tagGroupSetService;

    /**
     * * 获取一级及二级类目
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/getTags", method = RequestMethod.POST)
    public ResponseEntity<JSONObject> HomeTagProductList(HttpServletRequest request) {
        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.setContentType(MediaType.APPLICATION_JSON);
        HttpSession session = request.getSession();
        JSONObject jsonObject = new JSONObject();
        try {
            List<TagSet> tagSets = tagGroupSetService.selectTagAll();
            List<TagGroupSet> tagGroupSets = tagGroupSetService.selectAll();
            String str = JSONObject.toJSONString(tagSets);
            String s = JSONObject.toJSONString(tagGroupSets);
            jsonObject.put("tag", str);
            jsonObject.put("tagGroup", s);
            jsonObject = JSONObject.parseObject(JsonUtil.toErrJson(Error.OK, jsonObject.toJSONString()));
        } catch (Exception e) {
            jsonObject.put("Err", Error.OtherError);
            return new ResponseEntity<JSONObject>(jsonObject, responseHeaders, HttpStatus.OK);
        }
        return new ResponseEntity<JSONObject>(jsonObject, responseHeaders, HttpStatus.OK);
    }
}
